package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.adapter.ShippingAddressAdapter;
import com.redwine.app.net.GsonRequest;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.AddressListResponse;
import com.redwine.app.net.responses.SendMsgEvent;
import com.redwine.app.utils.Global;
import com.redwine.app.utils.NetUtil;
import com.redwine.app.view.MeasureListView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

import static com.redwine.app.utils.Utils.showToast;

/**
 * 收货地址
 * 作者：gassion on 17/9/9 16:10
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class ShippingAddressActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    private Intent intent;
    @ViewInject(R.id.address_measure_list_view)
    private MeasureListView mAddressMeasureListView;
    @ViewInject(R.id.address_ll_no_address)
    private LinearLayout mAddressLlNoAddress;
    private ShippingAddressAdapter shippingAddressAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shipping_address);
        ViewUtils.inject(this);
        EventBus.getDefault().register(this);
        initView();
        requestAddressListData();
    }

    private void initView() {
        headerTitle.setText(R.string.shipping_address);
        layoutBack.setVisibility(View.VISIBLE);
        shippingAddressAdapter = new ShippingAddressAdapter(this);
        mAddressMeasureListView.setAdapter(shippingAddressAdapter);
    }

    @OnClick({R.id.layout_back, R.id.address_ll_add})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;

            case R.id.address_ll_add:
                //添加收货地址
                if (!NetUtil.checkNet(this))
                    showToast(R.string.generic_check);
                intent = new Intent(ShippingAddressActivity.this, ShippingAddressAddActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 收获地址列表
     */
    private void requestAddressListData() {
        if (!NetUtil.checkNet(this)) {
            showToast(R.string.generic_check);
            return;
        }
        showWaiting();
        String url = NetworkConstants.ADDRESS_LIST_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", Global.user.getUid());
        GsonRequest<AddressListResponse> request = new GsonRequest<>(url, Request.Method.POST,
                AddressListResponse.class, false, params, new Response.Listener<AddressListResponse>() {
            @Override
            public void onResponse(AddressListResponse response) {
                dismissWaiting();
                if (response.isSuccess()) {
                    List<AddressListResponse.DataEntity> dataEntityList = response.getData();
                    if (null != dataEntityList && !dataEntityList.isEmpty()) {
                        mAddressLlNoAddress.setVisibility(View.VISIBLE);
                        shippingAddressAdapter.setData(dataEntityList);
                    } else {
                        mAddressLlNoAddress.setVisibility(View.GONE);
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissWaiting();
                showToast(R.string.generic_check);
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }

    public void onEventMainThread(SendMsgEvent event) {
        if (event.getSendType() == SendMsgEvent.REFURBISH_SHIPPING_ADDRESS) {
            requestAddressListData();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
